# -*- coding: utf-8 -*-
# created on 2017/1/16
# 

from mathsolver.functions.base import *
from sympy import Tuple
from mathsolver.functions.process import ProcessExpression


class XLSanJiaoXingKnownUpdate(BaseFunction):
    def solver(self, *args):
        known = self.known
        uEqs = args[0]
        triangle = args[1]
        new_known = dict(known)
        points = []
        for eq in uEqs:
            if isinstance(eq[1], (tuple, list)):
                expr = eq[0]
            else:
                if len(eq) == 2:
                    expr = eq[0] - eq[1]
                else:
                    expr = eq[0] - eq[2]
            p = r'vector\([A-Z][A-Z]\)'
            m = re.findall(p, str(expr))
            if m:
                for item in m:
                    new_item = sympify(item)
                    new_item_name = new_item.args[0]
                    if len(str(new_item_name)) == 2:
                        left_p, right_p = str(new_item_name)
                        points.append(left_p)
                        points.append(right_p)
        points = set(points)
        points = list(points)
        points_value = {}
        points_output = []
        for point in points:
            if point == triangle.point_A_name:
                points_value[point] = triangle.point_A_Axis.sympify()
            elif point == triangle.point_B_name:
                points_value[point] = triangle.point_B_Axis.sympify()
            elif point == triangle.point_C_name:
                points_value[point] = triangle.point_C_Axis.sympify()
            elif point == triangle.point_Gravity_name:
                points_value[point] = triangle.point_Gravity_Axis.sympify()
            else:
                lpoint_value = point + str("11")
                lpoint_value = sympify(lpoint_value)
                rpoint_value = point + str("22")
                rpoint_value = sympify(rpoint_value)
                points_value[point] = (lpoint_value, rpoint_value)
                points_output.append(BasePoint({"name": point, "value": [lpoint_value, rpoint_value]}))
        for eq in uEqs:
            if isinstance(eq[1], (tuple, list)):
                expr = eq[0]
            else:
                if len(eq) == 2:
                    expr = eq[0] - eq[1]
                else:
                    expr = eq[0] - eq[2]
            p = r'vector\([A-Z][A-Z]\)'
            m = re.findall(p, str(expr))
            if m:
                for item in m:
                    new_item = sympify(item)
                    new_item_name = new_item.args[0]
                    if len(str(new_item_name)) == 2:
                        left_p, right_p = str(new_item_name)
                        left_p_value = points_value[left_p]
                        right_p_value = points_value[right_p]
                        new_item_value = (right_p_value[0] - left_p_value[0], right_p_value[1] - left_p_value[1])
                        name = str('Axis_') + str(new_item_name)
                        new_known[name] = new_item_value
        self.steps.append(["", "设%s" % ",".join(["%s" % v.printing() for v in points_output])])
        self.output.append(new_known)
        return self


def xl_sanjiao_area_known_update(known, ueqs, triangle, target_points):
    new_known = dict(known)
    points = []
    for point in target_points:
        points.append(point)
    for eq in ueqs:
        if isinstance(eq[1], (tuple, list)):
            expr = eq[0]
        else:
            if len(eq) == 2:
                expr = eq[0] - eq[1]
            else:
                expr = eq[0] - eq[2]
        p = r'vector\([A-Z][A-Z]\)'
        m = re.findall(p, str(expr))
        if m:
            for item in m:
                new_item = sympify(item)
                new_item_name = new_item.args[0]
                if len(str(new_item_name)) == 2:
                    left_p, right_p = str(new_item_name)
                    points.append(left_p)
                    points.append(right_p)
    points = set(points)
    points = list(points)
    points_value = {}
    for point in points:
        if point == triangle.point_A_name:
            points_value[point] = triangle.point_A_Axis.sympify()
            new_known[point] = triangle.point_A_Axis
        elif point == triangle.point_B_name:
            points_value[point] = triangle.point_B_Axis.sympify()
            new_known[point] = triangle.point_B_Axis
        elif point == triangle.point_C_name:
            points_value[point] = triangle.point_C_Axis.sympify()
            new_known[point] = triangle.point_C_Axis
        elif point == triangle.point_Gravity_name:
            points_value[point] = triangle.point_Gravity_Axis.sympify()
            new_known[point] = triangle.point_Gravity_Axis
        else:
            lpoint_value = point + str("11")
            lpoint_value = sympify(lpoint_value)
            rpoint_value = point + str("22")
            rpoint_value = sympify(rpoint_value)
            points_value[point] = (lpoint_value, rpoint_value)
            new_known[point] = BasePoint({"name": point, "value": [lpoint_value, rpoint_value]})
    for eq in ueqs:
        if isinstance(eq[1], (tuple, list)):
            expr = eq[0]
        else:
            if len(eq) == 2:
                expr = eq[0] - eq[1]
            else:
                expr = eq[0] - eq[2]
        p = r'vector\([A-Z][A-Z]\)'
        m = re.findall(p, str(expr))
        if m:
            for item in m:
                new_item = sympify(item)
                new_item_name = new_item.args[0]
                if len(str(new_item_name)) == 2:
                    left_p, right_p = str(new_item_name)
                    left_p_value = points_value[left_p]
                    right_p_value = points_value[right_p]
                    new_item_value = (right_p_value[0] - left_p_value[0], right_p_value[1] - left_p_value[1])
                    name = str('Axis_') + str(new_item_name)
                    new_known[name] = new_item_value
    return new_known


class XLSanJiaoXingAxisConditionEqTransform(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        eqs = []
        if isinstance(eq[1], (list, Tuple, tuple)):
            left = eq[0]
            right = eq[1]
            new_left = ProcessExpression(self.known).solver(BaseValue(str(left))).output[0].sympify()
            eqs.append([new_left[0], right[0]])
            eqs.append([new_left[1], right[1]])
            self.output.append(BaseEqs(eqs))
        else:
            if len(eq) == 3:
                left, op, right = eq
                expr = left - right
                new_expr = ProcessExpression(self.known).solver(BaseValue(str(expr))).output[0].sympify()
                eqs.append([new_expr, op, S.Zero])
            else:
                expr = eq[0] - eq[1]
                new_expr = ProcessExpression(self.known).solver(BaseValue(str(expr))).output[0].sympify()
                if isinstance(new_expr, tuple):
                    eqs.append([new_expr[0], S.Zero])
                    eqs.append([new_expr[1], S.Zero])
                else:
                    eqs.append([new_expr, S.Zero])
            self.output.append(BaseIneqs(eqs))
        return self


if __name__ == '__main__':
    pass
